package cn.ilikes.tools.nosql.jpa.clients.indexs.index.lucenes.asytools;

import java.util.List;

import org.I0Itec.zkclient.IZkChildListener;
import org.slf4j.Logger;

import cn.ilikes.tools.logs.Loggerfactory;

public class AsyZookeeprLockListener implements IZkChildListener {
	
	private Logger logger = Loggerfactory.instance(AsyZookeeprLockListener.class);
	
	private String parentPath;
	
	private String localId;
	
	private ZookeeprLockListener zookeeprLockListener;
	
	public AsyZookeeprLockListener(String parentPath, String localId,ZookeeprLockListener zookeeprLockListener) {
		super();
		this.parentPath = parentPath;
		this.localId = localId;
		this.zookeeprLockListener = zookeeprLockListener;
	}

	@Override
	public void handleChildChange(String parentPaths, List<String> currentChilds) throws Exception {
		
		if(parentPath.trim().equals(parentPaths.trim()))
		{
			
			if(!currentChilds.isEmpty())
				{
				
				String lock= LockUilts.firstlocalLock(currentChilds);
				System.out.println("=================== handleChildChange =================  lock "+
					 lock+"    localId  "+localId+"    "+currentChilds.size())
						;
				if(!LockUilts.isLocalLock(localId,lock))return;
				if(Loggerfactory.isdebug(logger))Loggerfactory.debug(logger, "handle Child Change : "+lock);
				
				zookeeprLockListener.lock(parentPaths,lock  );
				}
		}

	}

}
